package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.core.domain.LItem;
import com.learn.core.domain.LOrganization;
import com.learn.core.mapper.LItemMapper;
import com.learn.core.service.ILItemService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LItemServiceImpl
 * @date 2021.03.16 14:50
 */
@Service
public class LItemServiceImpl extends BaseServiceImpl<LItemMapper, LItem> implements ILItemService {

    @Autowired
    LItemMapper lItemMapper;

    @Override
    public List<LItem> queryLItemAll(QueryWrapper<LItem> queryWrapper) {
        List<LItem> treeMenus =new  ArrayList<>();
        List<LItem> lItemList = lItemMapper.selectList(queryWrapper);
        // 查询树结构
        for(LItem lItem : getRootNode(lItemList)) {
            lItem=buildChilTree(lItem,lItemList);
            treeMenus.add(lItem);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LItem buildChilTree(LItem pNode, List<LItem> menuList){
        List<LItem> chilMenus =new ArrayList<>();
        for(LItem menuNode : menuList) {
            if(menuNode.getParentId() == pNode.getMenuId()) {
                chilMenus.add(buildChilTree(menuNode,menuList));
            }
        }
        pNode.setChilLItem(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<LItem> getRootNode(List<LItem> menuList) {
        List<LItem> rootMenuLists =new  ArrayList<LItem>();
        for(LItem menuNode : menuList) {
            if(menuNode.getParentId() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
